package com.tools.developer.app;

import java.lang.ref.SoftReference;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.developer.R;

public class AppListActivity extends ListActivity implements
		OnItemClickListener, OnClickListener {

	private static final int MSG_LOADING_LIST = 0x1001;
	private static final int MSG_REFRESH_LIST = 0x1002;

	private static View waitLayout;
	private static ListView mListV;
	private ListView catalogList;
	private int mCatalogType = AppModel.APP_TYPE_ALL;
	private AppCatalogAdapter mCatalogAdapter;
	private static ArrayList<AppModel> mAppList = new ArrayList<AppModel>();
	private MyHandler mHandler;
	private AppListThread mListThread;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_applist);
		((TextView) findViewById(R.id.TextViewtitle))
				.setText(R.string.app_title);
		ImageView leftImg = (ImageView) findViewById(R.id.title_left_image);
		leftImg.setImageResource(R.drawable.app_catalog);
		leftImg.setVisibility(View.VISIBLE);
		leftImg.setOnClickListener(this);
		waitLayout = findViewById(R.id.software_load_waiting_layout);
		waitLayout.setVisibility(View.VISIBLE);

		catalogList = (ListView) findViewById(R.id.app_catalog_list);
		mCatalogAdapter = new AppCatalogAdapter(this.getApplicationContext());
		catalogList.setAdapter(mCatalogAdapter);
		catalogList.setOnItemClickListener(this);

		mListV = (ListView) findViewById(android.R.id.list);
		mListV.setVisibility(View.INVISIBLE);
		mListV.setOnItemClickListener(this);
		mHandler = new MyHandler(this);
		mListThread = new AppListThread(mHandler, AppModel.APP_TYPE_ALL);
		mListThread.start();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	class AppListThread extends Thread {
		MyHandler mHandler;
		int mType;

		AppListThread(MyHandler handler, int type) {
			mHandler = handler;
			mType = type;
		}

		@Override
		public void run() {
			switch (mType) {
			case AppModel.APP_TYPE_ALL:
				mAppList = AppUtil.getInstalledAppList(AppListActivity.this);
				break;
			case AppModel.APP_TYPE_RUNNING:
				mAppList = AppUtil.getRunningAllAppList(AppListActivity.this);
				break;
			case AppModel.APP_TYPE_SYS:
				mAppList = AppUtil.getInstalledRomAppList(AppListActivity.this);
				break;
			default:
				mAppList = AppUtil.getInstalledAppList(AppListActivity.this);
				break;
			}
			mHandler.sendEmptyMessage(MSG_REFRESH_LIST);

		}
	}

	private static class AppListAdapter extends BaseAdapter {
		private ArrayList<AppModel> mAppList;
		private SoftReference<Activity> mActivity;

		public AppListAdapter(Activity activity, ArrayList<AppModel> appList) {
			mActivity = new SoftReference<Activity>(activity);
			mAppList = appList;
		}

		@Override
		public int getCount() {
			return mAppList.size();
		}

		@Override
		public AppModel getItem(int position) {
			return mAppList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (mActivity != null) {
				Activity activity = mActivity.get();
				if (activity != null) {
					ViewHolder holder = null;
					if (convertView == null) {
						convertView = activity.getLayoutInflater().inflate(
								R.layout.app_list_item, null, false);
						holder = new ViewHolder();
						holder.iconView = (ImageView) convertView
								.findViewById(R.id.icon);
						holder.titleView = (TextView) convertView
								.findViewById(R.id.title);
						holder.pkgName = (TextView) convertView
								.findViewById(R.id.pkg_name);
						holder.ver = (TextView) convertView
								.findViewById(R.id.app_ver);
						holder.shareUid = (TextView) convertView
								.findViewById(R.id.app_share_uid);
						convertView.setTag(holder);

					} else {
						holder = (ViewHolder) convertView.getTag();
					}

					AppModel app = getItem(position);
					PackageManager pm = activity.getPackageManager();
					try {
						holder.iconView.setImageDrawable(pm
								.getApplicationIcon(app.packageName));
					} catch (NameNotFoundException e) {
					}
					holder.titleView.setText(app.mLabel);
					holder.pkgName.setText(app.packageName);
					holder.ver.setText(app.mVer);
					holder.shareUid.setText(app.mShareUid);

				}
			}
			return convertView;
		}
	}

	private static class ViewHolder {
		ImageView iconView;
		TextView titleView;
		TextView pkgName;
		TextView ver;
		TextView shareUid;
	}

	static class MyHandler extends Handler {
		private AppListAdapter mListAdapter;
		private ListView mListView;
		private View mWaitLayout;
		private SoftReference<Activity> mActivity;

		private MyHandler(Activity activity) {
			mActivity = new SoftReference<Activity>(activity);
			mListView = (ListView) activity.findViewById(android.R.id.list);
			mWaitLayout = activity
					.findViewById(R.id.software_load_waiting_layout);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESH_LIST:
				if (mActivity != null) {
					if (mActivity.get() != null) {
						mListAdapter = new AppListAdapter(mActivity.get(),
								mAppList);
						mListView.setAdapter(mListAdapter);
						mWaitLayout.setVisibility(View.GONE);
						mListView.setVisibility(View.VISIBLE);
					}
				}
				break;

			case MSG_LOADING_LIST:
				mWaitLayout.setVisibility(View.VISIBLE);
				mListView.setVisibility(View.GONE);
				break;
			}
		}

	};

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent == mListV) {
			AppModel app = mAppList.get(position);
			if (app != null) {
				Intent intent = new Intent();
				switch (mCatalogType) {
				case AppModel.APP_TYPE_RUNNING:
					intent.putExtra(AppDetailActivity.APP_MODEL, app);
					intent.setClass(this, AppDetailActivity4Running.class);
					break;
				default:
					intent.putExtra(AppDetailActivity.APP_MODEL, app);
					intent.setClass(this, AppDetailActivity.class);
					break;
				}
				intent.putExtra(AppDetailActivity.PKG_NAME, app.packageName);
				this.startActivity(intent);
			}

		} else if (parent == catalogList) {
			Toast.makeText(this, mCatalogAdapter.mTitles.get(position),
					Toast.LENGTH_LONG).show();

			if (this.getResources().getString(R.string.app_catalog_all)
					.equalsIgnoreCase(mCatalogAdapter.mTitles.get(position))) {
				mCatalogType = AppModel.APP_TYPE_ALL;
				mHandler.sendEmptyMessage(MSG_LOADING_LIST);

				mListThread = new AppListThread(mHandler, AppModel.APP_TYPE_ALL);
				mListThread.start();
			} else if (this.getResources()
					.getString(R.string.app_catalog_running)
					.equalsIgnoreCase(mCatalogAdapter.mTitles.get(position))) {
				mCatalogType = AppModel.APP_TYPE_RUNNING;
				mHandler.sendEmptyMessage(MSG_LOADING_LIST);

				mListThread = new AppListThread(mHandler,
						AppModel.APP_TYPE_RUNNING);
				mListThread.start();
			} else if (this.getResources()
					.getString(R.string.app_catalog_sys_app)
					.equalsIgnoreCase(mCatalogAdapter.mTitles.get(position))) {
				mCatalogType = AppModel.APP_TYPE_SYS;
				mHandler.sendEmptyMessage(MSG_LOADING_LIST);

				mListThread = new AppListThread(mHandler, AppModel.APP_TYPE_SYS);
				mListThread.start();
			} else {
				mCatalogType = AppModel.APP_TYPE_ALL;
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_image:
			switch (catalogList.getVisibility()) {
			case View.VISIBLE:
				catalogList.setVisibility(View.GONE);

				break;
			case View.GONE:
			case View.INVISIBLE:
				catalogList.setVisibility(View.VISIBLE);
				break;
			}

			break;
		}

	}

}